package com.battle.project.order.mq.receive;

import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * direct 是 routingKye = bindingKey
 */
@Component
public class DirectReceiveMqHandler {

    private static Logger logger = LoggerFactory.getLogger(DirectReceiveMqHandler.class);


    @RabbitListener(
            bindings = {
                    @QueueBinding(
                            value = @Queue(value = "${battle.order.direct.queue.name:order_direct_queue}",
                                    durable = "${battle.order.direct.queue.durable:true}",
                                    autoDelete = "${battle.order.direct.queue.autoDelete:true}",
                                    exclusive = "${battle.order.direct.queue.exclusive:false}"),
                            exchange = @Exchange(value = "${battle.order.direct.exchange.name:order_direct_exchange}",
                                    type = "${battle.order.direct.exchange.type:direct}"),
                            key = "${battle.order.direct.routing.key:order}"
                    )
            })
    public void receiveMessage(Message message, Channel channel) {
        byte[] body = message.getBody();
        MessageProperties messageProperties = message.getMessageProperties();
        //
        long deliveryTag = messageProperties.getDeliveryTag();

        String messageStr = new String(body, StandardCharsets.UTF_8);
        logger.info("【direct】ReceiveMqHandler接收到消息message:{}", messageStr);
    }
}
